﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace JCodeManage
{
    public partial class CodeModify : Form
    {
       
        JCode.Model.CodeInfo model = new JCode.Model.CodeInfo();
     
        JCode.BLL.Code bll = new JCode.BLL.Code();
       
        JCode.BLL.Category bll_cate = new JCode.BLL.Category();
        public CodeModify()
        {
            InitializeComponent();
            //richcontent.KeyDown += new KeyEventHandler(richcontent_KeyDown);
            CreateTree();
           
        }
         //返回搜索字符
         public static string GetLastWord(string str,int i)
         {
             string x = str;
             Regex reg= new Regex(@"\s+[a-z]+\s*",RegexOptions.RightToLeft);
             x = reg.Match(x).Value;
 
             Regex reg2 = new Regex(@"\s");
             x = reg2.Replace(x, "");
             return x;
         }

        //设定颜色
         public static void MySelect(System.Windows.Forms.RichTextBox tb, int i, string s, Color c,bool font)
         {
             tb.Select(i - s.Length, s.Length);
             tb.SelectionColor = c;
             //是否改变字体
             if(font)
                 tb.SelectionFont = new Font("宋体", 12, (FontStyle.Bold));
             else
                 tb.SelectionFont = new Font("宋体", 12, (FontStyle.Regular));
               //以下是把光标放到原来位置，并把光标后输入的文字重置
             tb.Select(i,0);
             tb.SelectionFont = new Font("宋体", 12, (FontStyle.Regular));
             tb.SelectionColor = Color.Black;
         }


        void richcontent_KeyDown(object sender, KeyEventArgs e)
        {
            //throw new NotImplementedException();
             RichTextBox rich = (RichTextBox)sender;
            //throw new Exception("The method or operation is not implemented.");
            string s = GetLastWord(rich.Text, rich.SelectionStart);

            if (AllClass().IndexOf(s) > -1)
            {
                MySelect(rich, rich.SelectionStart, s, Color.CadetBlue, true);
            }

        }

        /// <summary>
        /// 修改的构造函数
        /// </summary>
        /// <param name="model">code实体类</param>
        public CodeModify(JCode.Model.CodeInfo model)
        {
            InitializeComponent();
            txttitle.Text = model.Title;
            richcontent.Text = model.Content;
            txtkeys.Text = model.Keys;
            CreateTree();
            btnadd.Text = "更新";
            comboBox1.SelectedItem = "1003";
        }

        private void btnadd_Click(object sender, EventArgs e)
        {
            item ss = (item)comboBox1.SelectedItem;
            model.Title = txttitle.Text;
            model.Content = richcontent.Text;
            model.Keys = txtkeys.Text;
            model.DateCreated = DateTime.Now;
            model.DateUpdate = DateTime.Now;
            model.CategoryID = int.Parse(ss.sID);
            bll.Add(model);
            
            
            MessageBox.Show("添加成功!");
        }

        

        private void CreateTree()
        {


            DataSet ds = bll_cate.GetList();
           
      
            AddItems("0", comboBox1, ds);
        }

        public static void AddItems(string parentId, ComboBox comb, DataSet ds)
        {
           
            DataView dv = new DataView(ds.Tables[0]);
            dv.RowFilter="[ParentID]='" + parentId + "'";
            foreach (DataRowView row in dv)
            {
                comb.Items.Add(new item(row["CategoryID"].ToString(),JCode.Common.Text.StringOfChar(int.Parse(row["Layer"].ToString()), "-") + row["CategoryName"].ToString()));
                AddItems(row["CategoryID"].ToString(), comb, ds);
            }
        }


        struct item
        {
            public string sID, sText;
            public item(string sID, string sText)
            {
                this.sID = sID;
                this.sText = sText;
            }
            public override string ToString()
            {
                return this.sText.ToString();
            }
            

        }

        private void button1_Click(object sender, EventArgs e)
        {
            richcontent.Select(0,5);
             richcontent.SelectionFont = new Font("宋体", 12, (FontStyle.Regular));
            richcontent.SelectionColor = Color.Blue;
           
        }

       
        /// <summary>
        /// 关键字
        /// </summary>
        /// <returns></returns>
        public static List<string> AllClass()
         {
             List<string> list = new List<string>();
             list.Add("function");
             list.Add("return");
             list.Add("class");
             list.Add("new");
             list.Add("extends");
             list.Add("var");
             return list;
         }
    }
}
